package com.heima.controller;

import com.heima.dao.StudentDao;
import com.heima.domian.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端传入的数据
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        //创建学生类对象，把接收到的值封装到对象中
        Student stu = new Student();
        stu.setName(name);
        stu.setAge(Integer.parseInt(age));
        stu.setScore(Integer.parseInt(score));
        //创建dao对象，把存入数据了的stu对象再存入到addStudent()方法
        StudentDao stud = new StudentDao();
        stud.addStudent(stu);
        //然后跳转到成功页面
        resp.getWriter().write("添加成功，2秒后跳转到首页");
        resp.setHeader("Refresh", "2;URL=" + "/index.jsp");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
